//
//  MCSinglePickerView.swift
//  StoreManage
//
//  Created by MC on 2018/2/1.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

protocol MCSinglePickerViewDelegate : NSObjectProtocol {
    func MCSinglePickerViewSelectedRow(type:Int,row:Int,str:String)
}

class MCSinglePickerView: UIView {

    
    weak var delegate : MCSinglePickerViewDelegate?
    
    var type = 0
    
    
    var dataArray : NSArray = NSArray() {
        didSet {
            if dataArray.count == 0 {
                return
            }
            
            showLabel.text = dataArray.firstObject as? String ?? "暂无"
            pickerView.reloadAllComponents()
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dismissSelf))
        self.addGestureRecognizer(tap)
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(200)
        }
        
        bgView.addSubview(cancelButton)
        cancelButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        bgView.addSubview(sureButton)
        sureButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-20)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        bgView.addSubview(showLabel)
        showLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(cancelButton.snp.right).offset(10)
            make.right.equalTo(sureButton.snp.left).offset(-10)
            make.centerY.equalTo(cancelButton)
        }
        
        bgView.addSubview(pickerView)
        pickerView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(sureButton.snp.bottom).offset(10)
            make.bottom.equalTo(-10)
        }
    }
    
    @objc func dismissSelf() {
        self.removeFromSuperview()
    }
    
    @objc func emptyEvent() {}
    
    @objc func sureButtonClicked() {
        self.removeFromSuperview()
        let row = pickerView.selectedRow(inComponent: 0)
        delegate?.MCSinglePickerViewSelectedRow(type: type, row: row, str: dataArray[row] as? String ?? "")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(emptyEvent))
        view.addGestureRecognizer(tap)
        
        return view
    }()

    lazy var cancelButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.layer.borderColor = UIColor.MCMain.cgColor
        button.layer.borderWidth = 1
        button.titleLabel?.font = MCFont(13)
        button.setTitle("取消", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.addTarget(self, action: #selector(dismissSelf), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var sureButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 3
        button.backgroundColor = UIColor.MCMain
        button.titleLabel?.font = MCFont(13)
        button.setTitle("确定", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(sureButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var showLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.black
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    lazy var pickerView: UIPickerView = {
        let view = UIPickerView()
        view.delegate = self
        view.dataSource = self
        view.selectedRow(inComponent: 0)
        view.showsSelectionIndicator = true
        view.backgroundColor = UIColor.groupTableViewBackground
        return view
    }()
}


extension MCSinglePickerView : UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return dataArray[row] as? String ?? "暂无"
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(component,row)
        if row >= dataArray.count {
            return
        }
        
        showLabel.text = dataArray[row] as? String ?? "暂无"
    }
}
